package cn.jiyun.controller;

import cn.jiyun.feignclient.FeignClient;
import cn.jiyun.pojo.Emp;
import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@RestController
public class EmpCotroller {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    FeignClient feignClient;

    @RequestMapping("consumer")
    //@HystrixCommand(fallbackMethod = "failMethod")
    public List<Emp> findEmpAll(){
        //List list = restTemplate.getForObject("http://localhost:8081/findAll", List.class);

        //List<ServiceInstance> instances = discoveryClient.getInstances("provider-server");
        //ServiceInstance serviceInstance = instances.get(0);

        //String url = "http://provider-server/findAll";
        //List list = restTemplate.getForObject(url, List.class);

        List<Emp> list = feignClient.findAll();
        return list;
    }


    @GetMapping("consumerId")
    public Emp findById(@RequestParam("id") Integer id){
        return feignClient.findEmpById(id);
    }

    @GetMapping("consumerIde/{id}")
    public Emp findByIde(@PathVariable("id") Integer id){
        return feignClient.findEmpById(id);
    }

    public List<Emp> failMethod(){
        List<Emp> list =new ArrayList<>();
        Emp emp =new Emp();
        emp.setEid(777);
        emp.setEname("访问超时，熔断测试");
        list.add(emp);
        return list;
    }


}
